<?php
/*
 * Team 1 Project - Palo Alto Bicycle Club
 * Author: Archana Nagarkatti
 * Class: COIN 71
 * Filename: announcement_list.php 
 * Date Created: 3/15/2009
 * Functionality: List all the announcements from the xml file 
 * and provides the user to post a new announcement
 * Modification History:
*/
?>
<?php
if( !headers_sent() )
{   header('Expires: ' . gmdate('D, d M Y H:i:s') . 'GMT');
    header('Cache-control: no-cache');
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<title>Palo Alto Bicycle Club</title>
	
        <script type="text/javascript" src="jquery/jquery-1.3.2.js"></script>
		<script type="text/javascript" src="_javascripts/announcement.js"></script> 
        <script type="text/javascript" src="ContentLoader.js"></script>
<script type="text/javascript">
	function validate()
	{
		var errorMessage = "";
		var titleValue = document.announce.title.value;
		var authorValue = document.announce.author.value;		
		var msgValue = document.announce.message.value;
		//title, author, message should not be empty
		if (titleValue.length < 1 || authorValue.length < 1 || msgValue.length < 1 )
		{
			errorMessage = "All fields should have a value to post an announcement.\n";
		}
		
		//message
		if(msgValue.length > 300)
		{
			errorMessage += 'Message length should be no greater than 300 characters.';
		}	
		if (errorMessage.length > 0)
		{
			alert (errorMessage);
			return false;
		}
	}
</script>
<link rel="stylesheet" type="text/css" href="_stylesheets/pabc.css" />
<link rel="stylesheet" type="text/css" href="datepicker/jquery.datepick.css" />
</head>
<body>

<!-- HEADER -->
	<?php include("header.php") ?>
<!-- end HEADER -->

<!-- WRAPPER -->
<div id="wrapper">

	<!-- LEFT COL -->
	<div class="leftcol">
		<?php include("leftcol.php") ?>
	</div>
	<!-- END LEFT COL -->

	
	<!-- RIGHT COL -->
	<div class="rightcol">
		<!-- ********* MAIN PAGE CONTENT *********** -->
        <div id="announcement">
        
            <h3>Announcements</h3>   

		</div>		
		<br />		
		<form method='POST' name='announce' action='<?php echo $PHP_SELF;?>' onsubmit='return validate();'>		
            <h3>Post an Announcement</h3>   

            <table id="post_table" rows="3" columns="2">
            <tr>
            <td>Title :</td>
            <td><input type="text" class="title" name="title" value="" size="40" maxlength="40" /></td>
            </tr>
            <tr>
            <td>Posted by :</td>
            <td><input type="text" class="author" name="author" value="" size="25" maxlength="25" /></td>
            </tr>
            <tr>
            <td>Message :</td>
            <td><textarea name="message" rows="5" cols="60" /></textarea>
            <br>Max: 300 char.  
            </tr>         
 			</table>
		<!-- ********* END MAIN CONTENT ************ -->
			<br/>
				<input type="submit" class="button" name="post_announce" value="Post" />
			<br/>
			<?php
			if (isset($_POST['post_announce'])) { // if page is submitted to itself echo the form							

				$XML_FILE = "announcements.xml";
                $dom = new DOMDocument;
				$dom->formatOutput = true;
                $dom->load($XML_FILE);
				$announcementsEl = $dom->getElementsByTagName('announcements')->item(0);

				//create announcement element
				$announceEl = $dom ->createElement('announcement');
				$announcementsEl -> appendChild($announceEl);
				//create title element				
				addChildWText($dom,  $announceEl, "title", $_POST["title"]);
				//create postedby element
				addChildWText($dom,  $announceEl, "author", $_POST["author"]);
				//create message element			
				addChildWText($dom,  $announceEl, "message", $_POST["message"]);
				//create post_date element			
				addChildWText($dom,  $announceEl, "postdate", date('m/d/Y'));
				
				//save XML file with newly posted information.
				$test = $dom->save($XML_FILE);
			}
			
			// reusing code in read_library.php
			// adds a child node "tagName" with "text" to "parent" in "document"
			function addChildWText ( $document, $parent, $tagName, $text ) {
			    $newTag = $document->createElement( $tagName );
			    $newTag->appendChild( $document->createTextNode( $text ));
			    $parent->appendChild( $newTag );
			}
			?>
		</form>
		</div>
		
		<!-- ********* END MAIN CONTENT ************ -->
		</div>
	</div>
    <!-- END RIGHT COLUMN -->

</div>
<!-- end WRAPPER -->

<!-- FOOTER -->
	<?php include("footer.php") ?>
<!-- END FOOTER -->

</body>
</html>            